<?php
define('IN_QY',true);
session_start();
require("include/common.inc.php");
require_once('config.php');
$code = $_GET['code'];
$state = $_GET['state'];
//换成自己的接口信息
$appid = 'wxd6671ed64906b4b4';
$appsecret = '04b2aa5188bde40a5adecb5fbaea226a';
if (empty($code)) $this->error('授权失败');
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

$token = json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
    echo "<script type='text/javascript'>location.href='wxlogin.php';</script>";
    exit;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token->errcode)) {
    echo "<script type='text/javascript'>location.href='wxlogin.php';</script>";
    exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//转成对象
$user_info = json_decode(file_get_contents($user_info_url),true);
//if (isset($user_info->errcode)) {
 //   echo '<h1>错误：</h1>'.$user_info->errcode;
 //   echo '<br/><h2>错误信息：</h2>'.$user_info->errmsg;
 //   exit;
//}
//打印用户信息
//echo '<pre>';
//print_r($user_info);
//echo '</pre>';
setcookie("wxheadimgurl",$user_info['headimgurl'],time()+(3600*12*1*1),"/");
setcookie("wxnickname",$user_info['nickname'],time()+(3600*12*1*1),"/");
setcookie("wxopenid",$user_info['unionid'],time()+(3600*12*1*1),"/");
 //判断开始
  $sql = "select * from tbl_user where wxopenid ='".$user_info['unionid']."'";
  $query = mysql_query($sql);
  $res = mysql_fetch_array($query);
  $wxxid = $res['wxopenid'];
if($res){
    setcookie("username",$res['userid'],time()+(3600*12*1*1),"/");
    setcookie("userid",$res['id'],time()+(3600*12*1*1),"/");
    setcookie("shuyu",$res['shuyu'],time()+(3600*12*1*1),"/"); 
    setcookie("addmima",strtotime($res['change_pwd_time']),time()+(3600*12*1*1),"/");  

	
	
	
		if($_COOKIE['popped']){
		echo "<script type='text/javascript'>location.href='".$_COOKIE['popped']."';</script>";		
		}else {	
		echo "<script type='text/javascript'>location.href='index.php';</script>";	
		}
	
	
	
    exit;
}
?>

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title><?=$user_info['nickname']?>.微信登录成功-众晒网</title>
    <link href="/ui/css/Hui.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        body { background: url(/ui/image/login_bg.jpg) no-repeat left top; background-size: cover; }
    </style>
</head>
<body class="H-height-100-percent H-box-sizing-border-box H-overflow-hidden">
    <div class="fix">
	    <div class="H-padding-vertical-bottom-50"></div>
        <div class="H-horizontal-center H-padding-vertical-both-25">
		
            <div class="H-text-align-center">
                <img src="<?=$user_info['headimgurl']?>" class="H-border-radius-12 H-margin-vertical-top-10" style="width:80px;height:80px;" />
				  <div class="H-padding-vertical-bottom-10"></div>
                <div class="H-font-size-18"><?=$user_info['nickname']?></div>
						  <div class="H-padding-vertical-bottom-10"></div>
				<div class="H-font-size-18">您还没有绑定众晒网账号</div>
			  <div class="H-font-size-18"><?=$wxxid?></div>
            </div>
        </div>
		
        <div class="H-border-vertical-top-after H-margin-vertical-top-10">
		
            <div class="H-padding-15 H-margin-vertical-top-10">
                <button tapmode="" onclick="H.openWin('wxcs', 'wxcs.php');" class="H-button H-width-100-percent  H-font-size-15 H-outline-none H-padding-vertical-both-15 H-padding-horizontal-both-20 H-theme-background-color9 H-theme-font-color-white H-theme-border-color9 H-theme-border-color9-click H-theme-background-color9-click H-theme-font-color9-click H-border-radius-3">有账号.去绑定</button>
            </div>
			
            <div class="H-padding-15 H-margin-vertical-top-10">
                <button tapmode="" onclick="H.openWin('mregister', 'mregister.php');" class="H-button H-width-100-percent  H-font-size-15 H-outline-none H-padding-vertical-both-15 H-padding-horizontal-both-20 H-theme-background-color9 H-theme-font-color-white H-theme-border-color9 H-theme-border-color9-click H-theme-background-color9-click H-theme-font-color9-click H-border-radius-3">没账号.去注册</button>
            </div>
			
            <div class="H-padding-15 H-margin-vertical-top-10">
                <button tapmode="" onclick="H.openWin('main.php', 'main.php');" class="H-button H-width-100-percent  H-font-size-15 H-outline-none H-padding-vertical-both-15 H-padding-horizontal-both-20 H-theme-background-color9 H-theme-font-color-white H-theme-border-color9 H-theme-border-color9-click H-theme-background-color9-click H-theme-font-color9-click H-border-radius-3">先跳过</button>
            </div>
			   <div class="H-padding-vertical-bottom-10"></div>		
        </div>
    </div>
    <div class="H-padding-vertical-bottom-10"></div>

    <script src="/ui/script/H.js" type="text/javascript"></script>
    <script type="text/javascript">
    </script>
</body>
</html>

